package cn.linkai.star.graph.core.service.impl;

import cn.linkai.star.graph.core.service.OllamaService;
import cn.linkai.star.graph.ollama.client.api.OllamaApi;
import cn.linkai.star.graph.ollama.client.pojo.OllamaChatRequest;
import cn.linkai.star.graph.ollama.client.pojo.OllamaChatRespone;
import cn.linkai.star.graph.ollama.client.pojo.OllamaMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import retrofit2.Call;
import retrofit2.Response;

import java.io.IOException;
import java.util.List;

@Service
public class OllamaServicelmpl implements OllamaService {

    @Autowired
    private OllamaApi ollamaApi;

    @Override
    public String translate(String prompt) {
        try {
            //构建消息体
            OllamaMessage message = new OllamaMessage ();
            message.setRole ("user");
            message.setContent ("请将以下内容翻译成英文(只用翻译成英文，不要过多回答问题):" + prompt);
            //构建请求
            OllamaChatRequest body = new OllamaChatRequest ();
            body.setModel ("qwen2.5:0.5b");
            body.setMessages (List.of (message));
            body.setStream (false);

            //发送请求
            Call<OllamaChatRespone> ollamaChatResponeCall = ollamaApi.chat (body);
            Response<OllamaChatRespone> response = ollamaChatResponeCall.execute ();
            if (response.isSuccessful ()){
                OllamaChatRespone messageBody = response.body ();
                return  (messageBody.getMessage ().getContent () );
            }
        } catch (IOException e) {
            e.printStackTrace ();
        }
        return prompt;
    }
}
